﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HT.Data.BussinessLogic;
using HT.Data;
using HT.Data.Objects;
using System.Web.Security;
using System.Data;
namespace Hotel
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                bool loginSucess = false;
                string errorMsg = string.Empty;
                DataSet dsUser = HotelManger.GetInstance().Authenticate(txtUName.Text.Trim(), txtPwd.Text.Trim());
                if (dsUser.Tables.Count > 0)
                {
                    if (dsUser.Tables[0].Rows.Count > 0)
                    {
                        if (Convert.ToBoolean(dsUser.Tables[0].Rows[0]["Active"]))
                        {
                            int compare = string.Compare(txtPwd.Text, dsUser.Tables[0].Rows[0]["password"].ToString());
                            if (compare == 0)
                            {

                                loginSucess = true;
                                //HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, txtUName.Text);
                                //Response.Cookies.Add(authCookie);
                                //HT.Data.Objects.User user = new HT.Data.Objects.User();
                                //user.UserId = Convert.ToInt32(dsUser.Tables[0].Rows[0]["UserId"]);
                                //user.UserName = dsUser.Tables[0].Rows[0]["Name"].ToString();
                                //user.RoleName = dsUser.Tables[0].Rows[0]["RoleName"].ToString();
                                //user.RoleID = Convert.ToInt32(dsUser.Tables[0].Rows[0]["RoleId"]);
                                //user.LoginID = dsUser.Tables[0].Rows[0]["LoginId"].ToString();
                                //user.Active = Convert.ToBoolean(dsUser.Tables[0].Rows[0]["Active"]);

                            }
                            else
                            {
                                errorMsg = "Invalid Password";
                            }
                        }
                        else
                        {

                            errorMsg = "Inactive User";
                        }


                    }
                    else
                    {
                        errorMsg = "Login Failed.";
                    }


                }
                if (loginSucess)
                {
                    FormsAuthenticationTicket tkt;
                    string cookiestr;
                    HttpCookie ck;
                    tkt = new FormsAuthenticationTicket(1, txtUName.Text, DateTime.Now,
                        DateTime.Now.AddMinutes(30), false, "your custom data");
                    cookiestr = FormsAuthentication.Encrypt(tkt);
                    ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
                    ck.Path = FormsAuthentication.FormsCookiePath;
                    Response.Cookies.Add(ck);
                   // Response.RedirectLocation = "Operations/RoomStatus.aspx";
                    Response.Redirect("Operations/RoomStatus.aspx");

                }
                else
                {

                    lblerror.Text = errorMsg;
                }

            }
            catch (Exception ex)
            {
               Response.Write(ex.Message.ToString());
            }
        }
    }
}